package com.unity3d.fragment;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import android.app.Fragment;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    BlankFragment1 blankFragment1=new BlankFragment1();
    BlankFragment2 blankFragment2=new BlankFragment2();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


//        一次性添加
        getSupportFragmentManager().beginTransaction()
                .add(R.id.constraintLayout,blankFragment1)
                .add(R.id.constraintLayout,blankFragment2)
                .hide(blankFragment1)
                .hide(blankFragment2)
                .commit();

        findViewById(R.id.button).setOnClickListener(
                (view)->{
//                    显示fragment1,隐藏fragment2
                    getSupportFragmentManager().beginTransaction()
                            .hide(blankFragment2)
                            .show(blankFragment1)
                            .commit();
                    //textView.setText("Activity");
                }
        );
        findViewById(R.id.button2).setOnClickListener(
                (view)->{
//                    显示fragment2,隐藏fragment1
                    getSupportFragmentManager().beginTransaction()
                            .hide(blankFragment1)
                            .show(blankFragment2)
                            .commit();
                }
        );
    }
}